<?php

// 热门市场

namespace app\shop\controller;

use think\Request;
use think\Controller;
use app\shop\common\GuessGoods;
use app\shop\model\Ad as AdModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;

class HotMarket extends Controller
{

    /**
     * 热门市场主页
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param ShopGoodsModel $shopGoods
     * @param AdModel $ad
     * @return mixed
     */
    public function index(Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods, AdModel $ad)
    {
        // 商品分类
        $type_lists = $goodsType->where(['parenTid' => '0', 'hot_recommend' => '1', 'status' => '1'])->field('id,title,ad_img,count,parenTid,parentStr,picUrl')->order(['sort' => 'desc'])->select();

        // 商品分类参数
        $inquire = $request->param();

        $condition['id'] = ['neq', 0];
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        if (!empty($inquire['tid'])) $condition['goods_str'] = ['like', '%,' . $inquire['tid'] . ',%'];

        // 商品数据
        $data = $shopGoods->where($condition)->limit(14)->select();

        // 猜你喜欢
        $guess_likes = (new GuessGoods)->guessLike();

        // 热门市场广告
        $ad = $ad->where('parenTid', 3)->find();


        return $this->fetch('', [
            'mate_head'   => ' 热门市场',
            'data'        => $data,
            'ad'          => $ad,
            'guess_likes' => $guess_likes,
            'type_likes'  => $type_lists,
        ]);

    }


    /**
     * 热门市场
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param AdModel $ad
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function details(Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods, AdModel $ad)
    {

        // 商品分类参数
        $inquire = $request->param();
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        // 商品分类
        $type_lists = '';
        if (!empty($inquire['id'])) {
            $type_lists = $goodsType->where('parenTid', $inquire['id'])->field('id,title,ad_img,count,parenTid,parentStr,picUrl')
                ->order(['sort' => 'desc'])->select();
            $condition['goods_str'] = ['like', '%,' . $inquire['id'] . ',%'];
        }

        if (!empty($inquire['tid'])) {

            // 商品分类
            $item = $goodsType::get($inquire['tid']);

            $type_lists = $goodsType->where('parenTid', $item['parenTid'])->field('id,title,ad_img,count,parenTid,parentStr,picUrl')
                ->order(['sort' => 'desc'])->select();
        }


        if (!empty($inquire['tid'])) $condition['goods_str'] = ['like', '%,' . $inquire['tid'] . ',%'];

        // 商品数据
        $data = $shopGoods->where($condition)->limit(14)->select();

        // 猜你喜欢
        $guess_likes = (new GuessGoods)->guessLike();

        // 热门市场广告
        $ad = $ad->where('parenTid', 3)->find();



        return $this->fetch('', [
            'mate_head'   => ' 热门市场',
            'ad'          => $ad,
            'data'        => $data,
            'type_list'   => $type_lists,
            'guess_likes' => $guess_likes,
        ]);
    }


    /**
     * 热门市场二级页面
     * @param $id
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param AdModel $ad
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function particulars($id, Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods, AdModel $ad)
    {
        // 商品分类
        $type_lists = $goodsType->where('parenTid', $id)->field('id,title,ad_img,count,parenTid,parentStr,picUrl')
            ->order(['sort' => 'desc'])->select();

        // 商品分类参数
        $inquire = $request->param();

        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        $condition['goods_str'] = ['like', '%,' . $id . ',%'];
        if (!empty($inquire['tid'])) $condition['goods_str'] = ['like', '%,' . $inquire['tid'] . ',%'];

        // 商品数据
        $data = $shopGoods->where($condition)->limit(14)->select();

        // 猜你喜欢
        $guess_likes = (new GuessGoods)->guessLike();

        // 获取商品分类信息
        // $type_arr = $goodsType->where(['status' => 1])->field('id,parenTid,title')->select();
        // $menu = get_cate($type_arr);
        // 热门市场广告
        $ad = $ad->where('parenTid', 3)->find();

        return $this->fetch('', [
            'mate_head'   => ' 热门市场',
            'data'        => $data,
            'ad'          => $ad,
            'guess_likes' => $guess_likes,
            'type_likes'  => $type_lists,
        ]);


    }


    /**
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param AdModel $ad
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function circumstance(Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods, AdModel $ad)
    {
        $inquire = $request->param();
        // 商品分类
        $item = $goodsType::get($inquire['id']);

        $type_lists = $goodsType->where('parenTid', $item['parenTid'])->field('id,title,ad_img,count,parenTid,parentStr,picUrl')
            ->order(['sort' => 'desc'])->select();

        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        $condition['goods_str'] = ['like', '%,' . $inquire['id'] . ',%'];
        if (!empty($inquire['tid'])) $condition['goods_str'] = ['like', '%,' . $inquire['tid'] . ',%'];

        // 商品数据
        $data = $shopGoods->where($condition)->limit(14)->select();

        // 猜你喜欢
        $guess_likes = (new GuessGoods)->guessLike();

        // 热门市场广告
        $ad = $ad->where('parenTid', 3)->find();


        return $this->fetch('', [
            'mate_head'   => ' 热门市场',
            'data'        => $data,
            'ad'          => $ad,
            'guess_likes' => $guess_likes,
            'type_likes'  => $type_lists,
        ]);

    }
}